package com.example.service.impl;

import com.example.dao.NewsDao;
import com.example.entity.News;
import com.example.service.NewsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * 新闻服务实现类
 */
@Service
public class NewsServiceImpl implements NewsService {

    @Resource
    private NewsDao newsDao;

    @Override
    public PageInfo<News> getNewsList(Integer pageNum, Integer pageSize, String key) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("key", key);
        
        PageHelper.startPage(pageNum, pageSize);
        List<News> list = newsDao.selectAll(map);
        return new PageInfo<>(list);
    }

    @Override
    public void saveOrUpdate(News news) {
        if (news.getId() == null) {
            newsDao.add(news);
        } else {
            newsDao.update(news);
        }
    }

    @Override
    public HashMap<String, Object> getNewsDetail(int id) {
        HashMap<String, Object> res = new HashMap<>();
        
        // 获取新闻详情
        News news = newsDao.findById(id);
        
        // 查询其它资讯信息
        List<News> list = newsDao.selectAll(null);
        
        res.put("news", news);
        res.put("list", list);
        
        return res;
    }

    @Override
    public void deleteNews(int id) {
        newsDao.delete(id);
    }

    @Override
    public void batchDeleteNews(String vals) {
        String[] val = vals.split(",");
        for (String id : val) {
            newsDao.delete(Integer.parseInt(id));
        }
    }
} 